package com.sanmubird.mywebframework.indicator.infrastructure;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sanmubird.mywebframework.indicator.domain.gateway.IndicatorGateway;
import com.sanmubird.mywebframework.indicator.domain.service.IndicatorConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author sam
 * @date 2021/10/11 18:36
 */
@Service
public class IndicatorGatewayImpl implements IndicatorGateway {


    @Autowired
    private IndicatorConfigMapper indicatorConfigMapper;


    /**
     * 根据 variableCode 提供 IndicatorConfig
     *
     * @param variableCode variableCode
     * @return IndicatorConfig
     */
    @Override
    public IndicatorConfig supplyIndicatorConfigBy(String variableCode) {
        LambdaQueryWrapper<IndicatorConfigPo> queryWrapper = Wrappers.lambdaQuery(IndicatorConfigPo.class);
        queryWrapper.eq(IndicatorConfigPo::getIndicatorCode, variableCode);
        IndicatorConfigPo configPo = indicatorConfigMapper.selectOne(queryWrapper);
        if (Objects.nonNull(configPo)) {
            return IndicatorConfig.builder()
                    .indicatorCode(configPo.getIndicatorCode())
                    .indicatorName(configPo.getIndicatorName())
                    .config(configPo.getConfig())
                    .valueType(configPo.getValueType())
                    .build();
        }
        return new IndicatorConfig();
    }
}
